//
//  KLSGRecommendViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGRecommendViewController.h"
#import "AppDelegate.h"
#import "KLSGRecommendTool.h"
#import "KLSGRecomendCell.h"
#import "KLSGRecommendheader.h"
#import "KLSGKindViewController.h"
#import "KLSGBrifAndVoiceViewController.h"
#import "KLSGMainViewController.h"
#import "KLSGDataFail.h"
#import "DDIndicator.h"

@interface KLSGRecommendViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIImageView *cycleImageView;
@property (nonatomic, strong) KLSGRecommendTool *tool;
@property (nonatomic, strong) KLSGDataFail *failView;

// 加载指示器
@property (nonatomic, strong) DDIndicator *indicator;

@end

@implementation KLSGRecommendViewController

// 懒加载
- (KLSGDataFail *)failView
{
    if (!_failView) {
        _failView = [[KLSGDataFail alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _failView;
}

- (KLSGRecommendTool *)tool
{
    if (!_tool) {
        _tool = [[KLSGRecommendTool alloc] init];
    }
    return  _tool;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self addSubView];
}

// 加载数据
- (void)setupData
{
    [self.failView hidden];
    [self.tool updataWithBlack:^{
        [self.indicator stopAnimating];
        [self.collectionView reloadData];
        [self showBannerView];
    } withFailBlock:^{
        [self.indicator stopAnimating];
        [self.failView netRequestFailWithBlock:^{
            [self.indicator startAnimating];
            [self setupData];
        }];
        [self.collectionView addSubview:self.failView];
    }];
}

#pragma mark -重写继承自父类的方法
// 本视图将要消失时 触发的方法
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 获取AppDelegate
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSliderVC setPanEnabled:NO];
}

// 本视图将要显示时触发的方法
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSliderVC setPanEnabled:YES];
    self.tabBarController.tabBar.hidden = NO;
    
    if (self.tool.titles.count == 0 ) {
    // 加载指示器启动
    self.indicator = [[DDIndicator alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.indicator.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
    [self.indicator startAnimating];
    [self.view addSubview:self.indicator];
    // 加载数据
    [self setupData];
    }
}

#pragma mark -布局子视图
- (void)addSubView
{
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"head"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftSliderAction:)];
    [leftBtn setTintColor:[UIColor colorWithRed:234 / 256.0 green:79 / 256.0 blue:56 / 256.0 alpha:1]];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 13;
    layout.minimumLineSpacing = 13;
    layout.itemSize = CGSizeMake((kScreenWidth - 60) / 3, 140);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 设置表头的高度
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 30);
    layout.sectionInset = UIEdgeInsetsMake(13, 13, 13, 13);
    
    // 添加collectionview
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.contentInset = UIEdgeInsetsMake(180, 0, 0, 0);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    // 在collectionview上添加imageview
    self.cycleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -180, kScreenWidth, 180)];
    [self.collectionView addSubview:self.cycleImageView];
    // 注册cell
    [self.collectionView registerClass:[KLSGRecomendCell class] forCellWithReuseIdentifier:@"cell"];
    
    // 注册表头
    [self.collectionView registerClass:[KLSGRecommendheader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}

#pragma mark -添加导航栏按钮事件
- (void)leftSliderAction:(UIBarButtonItem *)sender
{
    // 获得AppDelegate的对象
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.leftSliderVC.closed)
    {
        // 显示左视图
        [tempAppDelegate.leftSliderVC openLeftView];
    }
    else
    {
        // 关闭左视图
        [tempAppDelegate.leftSliderVC closeLeftView];
    }
    
}

#pragma mark -展示轮播图
- (void)showBannerView
{
    NSMutableArray *urls = [NSMutableArray array];
    for (KLSGRecommendModel *model in self.tool.banner) {
        [urls addObject:model.pic];
    }
    SDCycleScrollView *scrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
    [scrollView setImageURLStringsGroup:urls];
    [self.cycleImageView addSubview:scrollView];
    self.cycleImageView.userInteractionEnabled = YES;
}

#pragma mark -实现UICollectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = [self.tool.hotDic valueForKey:self.tool.titles[section]];
    return array.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  self.tool.titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGRecomendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    KLSGRecommendModel *model = [self.tool getModelWithIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"zhanweitu"];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.coverLarge]];
    cell.titleLabel.text = model.title;
    cell.trackTitleLabel.text = model.trackTitle;
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    KLSGRecommendheader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    headerView.titleLabel.text = self.tool.titles[indexPath.section];
    headerView.button.tag = indexPath.section + 100;
    [headerView.button addTarget:self action:@selector(headerViewAction:) forControlEvents:UIControlEventTouchUpInside];
    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGBrifAndVoiceViewController *brifAndVoiceVC = [[KLSGBrifAndVoiceViewController alloc] init];
    KLSGRecommendModel *model = [self.tool getModelWithIndexPath:indexPath];
    brifAndVoiceVC.albumIdString = [NSString stringWithFormat:@"%@", model.albumId];
    KLSGKindModel *kindModel = [[KLSGKindModel alloc] init];
    kindModel.albumCoverUrl290 = model.coverLarge;
    kindModel.title = model.title;
    kindModel.intro = model.trackTitle;
    kindModel.playsCounts = model.playsCounts;
    kindModel.tracksCounts = model.tracks;
    kindModel.albumId = model.albumId;
    brifAndVoiceVC.model = kindModel;
    [self.navigationController pushViewController:brifAndVoiceVC animated:YES];
}

#pragma mark -按钮响应事件
- (void)headerViewAction:(UIButton *)sender
{
    NSInteger tags = sender.tag;
    if (tags == 100) {
        KLSGEditorRecommendViewController *editorVC = [[KLSGEditorRecommendViewController alloc] init];
        [self.navigationController pushViewController:editorVC animated:YES];
    } else {
        NSString *title = self.tool.titles[tags - 100];
        
        KLSGKindViewController *kindVC = [[KLSGKindViewController alloc] init];
        [self addChildViewController:kindVC];
        kindVC.Id = self.tool.categorys[title];
        NSString *titles = [title substringFromIndex:1];
        kindVC.titles = titles;
        [self.navigationController pushViewController:kindVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
